package com.huiztech.maystall.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.huiztech.maystall.http.AsyncHttpClient;
import com.huiztech.maystall.http.AsyncHttpResponseHandler;
import com.huiztech.maystall.http.RequestParams;
import com.huiztech.maystall.mvc.Controller;
import com.huiztech.maystall.mvc.MaystallView;

public class ShoppingCartActivity extends BaseActivity {

    private ListView listView;
    private List<Map<String, Object>> list;
    private CheckBoxAdapter adapter;

    private CheckBox checkBox;
    private Boolean isChecked = false;

    private Button deleteBtn,clearingBtn;
    private AlertDialog dialog;

    private LayoutParams params;
    
    private Controller controller;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_cart);

        controller = new Controller(new MaystallView(), this);
        
        deleteBtn = (Button) findViewById(R.id.shopping_cart_delete_btn);
        deleteBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                deleteProduct();
            }
        });
        
        clearingBtn = (Button) findViewById(R.id.shopping_cart_clearing_btn);
        clearingBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ShoppingCartActivity.this, WriteOrderActivity.class);
                startActivity(intent);
            }
        });

        listView = (ListView) findViewById(R.id.list);
        list = new ArrayList<Map<String, Object>>();
        adapter = new CheckBoxAdapter(this);
        listView.setAdapter(adapter);
        getData();

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Intent intent = new Intent(ShoppingCartActivity.this, ProductDetailActivity.class);
                startActivity(intent);
            }
        });

        checkBox = (CheckBox) findViewById(R.id.shopping_cart_total_checkbox);
        checkBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!isChecked) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).put("checkbox", true);
                    }
                    isChecked = true;
                    adapter.notifyDataSetChanged();

                } else {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).put("checkbox", false);
                    }
                    isChecked = false;
                    adapter.notifyDataSetChanged();
                }
            }
        });

    }

    private void getData() {
        list = controller.getShoppingCart();
        adapter.notifyDataSetChanged();
    }

    public class CheckBoxAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        class ViewHolder {
            ImageView imageView;
            TextView nameTextView, numberTextView;
            CheckBox checkBox;
        }

        public CheckBoxAdapter(Context context) {
            super();
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.shopping_cart_list_item, null);
                holder.imageView = (ImageView) convertView.findViewById(R.id.shop_list_item_image);
                holder.nameTextView = (TextView) convertView.findViewById(R.id.shop_list_item_name);
                holder.numberTextView = (TextView) convertView.findViewById(R.id.shop_list_item_number);
                holder.checkBox = (CheckBox) convertView.findViewById(R.id.shop_list_item_checkbox);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            WindowManager wm = (WindowManager) ShoppingCartActivity.this.getSystemService(Context.WINDOW_SERVICE);
            int windowsWidth = wm.getDefaultDisplay().getWidth();
            
            int widthUp = (int) (windowsWidth * 0.28);
            int heightUp = (int) (widthUp * 0.8);

            params = (LayoutParams) holder.imageView.getLayoutParams();
            params.width = widthUp;
            params.height = heightUp;
            holder.imageView.setLayoutParams(params);
            
            holder.imageView.setBackgroundResource(Integer.valueOf(list.get(position).get("image").toString()));
            holder.imageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(ShoppingCartActivity.this, ProductDetailActivity.class);
                    intent.putExtra("image", list.get(position).get("image").toString());
                    startActivity(intent);
                }
            });

            holder.nameTextView.setText(list.get(position).get("name").toString());
            holder.numberTextView.setText("price：" + list.get(position).get("price").toString());

            holder.checkBox.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    list.get(position).put("checkbox", cb.isChecked());
                }
            });

            holder.checkBox.setChecked(((Boolean) list.get(position).get("checkbox")));

            return convertView;
        }

    }

    private void deleteProduct() {
        dialog = new AlertDialog.Builder(ShoppingCartActivity.this).create();
        dialog.show();
        dialog.getWindow().setContentView(R.layout.delete_dialog);
        dialog.getWindow().findViewById(R.id.delete_dialog_cancel_btn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.getWindow().findViewById(R.id.delete_dialog_submit_btn).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // list反向删除
                for (int i = list.size() - 1; i >= 0; i--) {
                    if (((Boolean) list.get(i).get("checkbox"))) {
                        list.remove(i);
                    }
                }

                adapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });

    }
}
